package com.example.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.activity.Utils.DateUtil;

public class ResponseActivity extends AppCompatActivity implements View.OnClickListener {

    private final String back_msg = "还没睡呢";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_response);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        TextView tv_response = findViewById(R.id.tv_response);
        //拿到数据
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String request_time = bundle.getString("request_time");
            String request_content = bundle.getString("request_content");
            String desc = String.format("收到请求消息:\n请求时间:%s\n请求内容:%s", request_time, request_content);
            tv_response.setText(desc);
        }
        Button btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        //
        TextView tv_back = findViewById(R.id.tv_back);
        tv_back.setText("待返回的消息"+back_msg);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_time", DateUtil.getNowTime());
        bundle.putString("response_content", back_msg);
        intent.putExtras(bundle);
        setResult(ResponseActivity.RESULT_OK,intent);
        finish();
    }
}